package cn.iocoder.yudao.module.indicator.service.index;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.module.indicator.domain.dto.IndicatorWarnStyleDto;
import cn.iocoder.yudao.module.indicator.domain.entity.IndicatorWarn;
import cn.iocoder.yudao.module.indicator.domain.entity.WarnRule;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.indicator.dao.IndicatorWarn.IndicatorWarnMapper;
import cn.iocoder.yudao.module.indicator.dao.IndicatorWarn.IndicatorWarnStyleMapper;
import cn.iocoder.yudao.module.indicator.dao.IndicatorWarn.WarnRuleDeptMapper;
import cn.iocoder.yudao.module.indicator.dao.IndicatorWarn.WarnRuleMapper;
import cn.iocoder.yudao.module.indicator.domain.dto.IndicatorWarnDto;
import cn.iocoder.yudao.module.indicator.domain.entity.WarnRuleDept;
import cn.iocoder.yudao.module.indicator.domain.vo.IndicatorWarnVo;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author hp
 */
@Service
public class IndicatorWarnServiceImpl implements IndicatorWarnService {

    @Autowired
    IndicatorWarnMapper indicatorWarnMapper;
    @Autowired
    IndicatorWarnStyleMapper indicatorWarnStyleMapper;
    @Autowired
    WarnRuleMapper warnRuleMapper;
    @Autowired
    WarnRuleDeptMapper warnRuleDeptMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertIndicatorWarn(IndicatorWarnDto indicatorWarnDto) {
        //新增基础信息表
        IndicatorWarn indicatorWarn = BeanUtils.toBean(indicatorWarnDto, IndicatorWarn.class);
        indicatorWarnMapper.insert(indicatorWarn);
        //新增方式表
        List<IndicatorWarnStyleDto> indicatorWarnSetupModelList = indicatorWarnDto.getIndicatorWarnSetupModelList();
        indicatorWarnSetupModelList.stream().forEach(result -> {
            result.setWarnId(indicatorWarn.getId());
        });
        for (IndicatorWarnStyleDto indicatorWarnStyleDto : indicatorWarnSetupModelList) {
            indicatorWarnStyleMapper.insert(indicatorWarnStyleDto);
            //新增规则表
            List<WarnRule> warnRuleModelList = indicatorWarnStyleDto.getWarnRuleModelList();
            warnRuleModelList.stream().forEach(result -> {
                result.setWarnStyleId(indicatorWarnStyleDto.getId());
                if(Objects.equals(result.getSymbol(),"区间内")||Objects.equals(result.getSymbol(),"区间外")){
                    result.setMinValue(result.getThreshold().split("-")[0]);
                    result.setMaxValue(result.getThreshold().split("-")[1]);
                }
                warnRuleMapper.insert(result);
                if(Objects.equals(result.getTargetAudience(),"科室")){
                    //新增规则对应科室
                    warnRuleDeptMapper.insertBatch(rulesAndDepartmentBinding(result.getRuleDept(),result.getId()));
                }
            });
        }
    }
    /**
     * 规则和科室绑定
     */
    public   List<WarnRuleDept>  rulesAndDepartmentBinding(String[] deptList, Long warmSetupId){

        //在重新绑定
        List<WarnRuleDept> sysRuleDeptList = new ArrayList<WarnRuleDept>();
        for (String s : deptList) {
            WarnRuleDept sysRuleDept = new WarnRuleDept();
            sysRuleDept.setDeptId(s);
            sysRuleDept.setWarnRuleId(warmSetupId);
            sysRuleDeptList.add(sysRuleDept);
        }
        return sysRuleDeptList;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIndicatorWarn(IndicatorWarnDto indicatorWarn) {
        //删除基础信息
        indicatorWarnMapper.delete("ID",indicatorWarn.getId());
        //删除预警方式
        List<IndicatorWarnStyleDto> warnStylePos = indicatorWarnStyleMapper.selectList("WARN_ID", indicatorWarn.getId());
        Long warnStyleId = warnStylePos.get(0).getId();
        indicatorWarnStyleMapper.delete("WARN_ID",indicatorWarn.getId());
        //删除规则
        warnRuleMapper.delete("WARN_STYLE_ID", String.valueOf(warnStyleId));
        //删除绑定的科室数
        warnRuleDeptMapper.delete("WARN_RULE_ID",String.valueOf(warnStyleId));
        //重新调用新增
        ((IndicatorWarnServiceImpl)AopContext.currentProxy()).insertIndicatorWarn(indicatorWarn);
    }

    @Override
    public void deleteIndicatorWarn(String id) {
        //删除基础信息
        indicatorWarnMapper.delete("ID",id);
        //删除预警方式
        List<IndicatorWarnStyleDto> warnStylePos = indicatorWarnStyleMapper.selectList("WARN_ID", id);
        Long warnStyleId = warnStylePos.get(0).getId();
        indicatorWarnStyleMapper.delete("WARN_ID",id);
        //删除规则
        warnRuleMapper.delete("WARN_STYLE_ID", String.valueOf(warnStyleId));
        //删除绑定的科室数
        warnRuleDeptMapper.delete("WARN_RULE_ID",String.valueOf(warnStyleId));
    }

    @Override
    public PageResult<IndicatorWarn> queryIndicatorsWarnList(IndicatorWarnDto indicatorWarn) {
        return indicatorWarnMapper.selectPage(indicatorWarn);
    }

    @Override
    public IndicatorWarnVo queryWarnRule(String id) {
        //查询基础信息
        IndicatorWarn indicatorWarn1 = indicatorWarnMapper.selectOne("ID", id);
        //查询预警设置方式
        List<IndicatorWarnStyleDto> indicatorWarnStyleDtoList = indicatorWarnStyleMapper.selectList("WARN_ID", indicatorWarn1.getId());
        for (IndicatorWarnStyleDto indicatorWarnStyleDto : indicatorWarnStyleDtoList) {
            //设置对应方式的规则
            List<WarnRule> warnRuleList = warnRuleMapper.selectList("WARN_STYLE_ID", indicatorWarnStyleDto.getId());
            indicatorWarnStyleDto.setWarnRuleModelList(warnRuleList);
            warnRuleList.forEach(rule ->{
                List<WarnRuleDept> warnRuleDeptList = warnRuleDeptMapper.selectList("WARN_RULE_ID", rule.getId());
                rule.setRuleDept(warnRuleDeptList.stream().map(WarnRuleDept::getDeptId)
                        .toArray(String[]::new));
            });
        }
        IndicatorWarnVo indicatorWarnVo =  new IndicatorWarnVo();
        BeanUtil.copyProperties(indicatorWarn1,indicatorWarnVo);
        indicatorWarnVo.setIndicatorWarnSetupModelList(indicatorWarnStyleDtoList);
        return indicatorWarnVo;
    }

}
